/* uncomment this block if there are some conflicts with other js library/framework */
//var $j = jQuery.noConflict();

//var $j = jQuery;

//(function ($) {

var App = {
    /**
    * setting for apps
    */
    settings: {
        /**
        * Default path for loading animation image.
        */
        //loadingImage: "/Content/images/loadingAnimation.gif"
		loadingImage: "/assets/image/loadingAnimation.gif"
    },
    /**
    * Reload the current page.
    */
    reload: function () {
        window.location.reload();
    },
    /**
    * Create empty dialog.
    * Need to include jquery ui dialog.
    */
    dialog: function (html, title, onClose, options) {
        if (onClose == undefined || typeof onClose != 'function')
            onClose = function () { };
        if (options == undefined || typeof options != 'object')
            options = {};
        if (html == undefined)
            html = "";
        if (title == undefined || typeof title != 'string')
            title = "Title";

        var dialog = {
            element: $('<div></div>').html(html).css({
                'min-width': '250px'
            }),
            dismiss: function () {
                this.element.dialog('close');
            },
            setTitle: function (title) {
                this.element.dialog('option', 'title', title);
            },
            setMessage: function (message) {
                if(message == undefined || typeof message != 'string')
                    message = '';
                message = $('<span></span>').text(message).html().replace('\n', '<br />');
                this.element.find('div[rel="caption"]').html(message);
            }
        };
        dialog.element.dialog({
            height: 'auto',
            width: 'auto',
            resizable: false,
            modal: true,
            draggable: true,
            title: title,
            close: onClose,
            buttons: options
        });
        return dialog;
    },
    /**
    * Create loading dialog object.
    * Need to include jquery ui dialog.
    */
    loading: function (message, title, onClose) {
        if (message == undefined || typeof message != 'string')
            message = 'Loading please wait...';
        if (title == undefined || typeof title != 'string')
            title = 'Loading';
        message = $('<span></span>').text(message).html().replace(/\n/g, '<br />');
        var text = $('<div align="left"></div>').attr('rel', 'caption').css({
            'padding-top': '10px'
        }).html(message);
        var loadImage = $('<div align="center"></div>').css({
            'margin-top': '10px'
        }).append(
            $('<img />').attr('src', App.settings.loadingImage)
        )
        var dialog = App.dialog($('<div></div>').append(text).append(loadImage), title, onClose);
        return dialog;
    },
    /**
    * Create alert dialog.
    * Need to include jquery ui dialog.
    */
    alert: function (message, title, onClose) {
        if (message == undefined || typeof message != 'string')
            message = 'Message';
        if (title == undefined || typeof title != 'string')
            title = 'Alert';
        message = $('<span></span>').text(message).html().replace(/\n/g, '<br />');
        var text = $('<div align="left"></div>').attr('rel', 'caption').css({
            'padding-top': '10px'
        }).html(message);
        var dialog = App.dialog(text, title, onClose, {
            'Ok': function () {
                $(this).dialog('close');
            }
        });
        return dialog;
    },
    /**
    * Create confirm dialog.
    * Need to include jquery ui dialog.
    */
    confirm: function (message, title, onYes, onNo, onClose) {
        if (message == undefined || typeof message != 'string')
            message = 'Are you sure?';
        if (title == undefined || typeof title != 'string')
            title = 'Confirm';
        if (onYes == undefined || typeof onYes != 'function')
            onYes = function () { };
        if (onNo == undefined || typeof onNo != 'function')
            onNo = function () { };
        message = $('<span></span>').text(message).html().replace(/\n/g, '<br />');
        var text = $('<div align="left"></div>').attr('rel', 'caption').css({
            'padding-top': '10px'
        }).html(message);
        var dialog = App.dialog(text, title, onClose, {
            'Yes': function () {
                $(this).dialog('close');
                onYes();
            },
            'No': function () {
                $(this).dialog('close');
                onNo();
            }
        });
        return dialog;
    },
    prompt: function (message, title, defaultMessage, onConfirm, onCancel, onClose) {
        if (message == undefined || typeof message != 'string')
            message = 'Input:';
        if (title == undefined || typeof title != 'string')
            title = 'Input';
        if (defaultMessage == undefined || typeof defaultMessage != 'string')
            defaultMessage = '';
        if (onConfirm == undefined || typeof onConfirm != 'function')
            onConfirm = function (value) { };
        if (onCancel == undefined || typeof onCancel != 'function')
            onCancel = function () { };

        message = $('<span></span>').text(message).html().replace(/\n/g, '<br />');
        var text = $('<div align="left"></div>').attr('rel', 'caption').css({
            'padding-top': '10px'
        }).html(message);
        var textfield = $('<div align="left"></div>').css({
            'margin-top': '10px'
        }).append(
            $('<input type="text" />').attr('rel', 'app-input').css({
                'width': '250px'
            }).val(defaultMessage)
        );
            var dialog = App.dialog($('<form></form>').append(text).append(textfield).submit(function () {
                var value = $(this).find('input[type="text"][rel="app-input"]').val();
            dialog.element.dialog('close');
            onConfirm(value);
            return false;
        }), title, onClose, {
            'Ok': function () {
                var value = $(this).find('input[type="text"][rel="app-input"]').val();
                $(this).dialog('close');
                onConfirm(value);
            },
            'Cancel': function () {
                $(this).dialog('close');
                onCancel();
            }
        });
        return dialog;
    }
};


/* uncomment this block if there are some conflicts with other js library/framework */
//})(jQuery);